#-*- Makefile -*-
## This now is use to compile the standard packages, only

all:  makeMakedeps $(DLLNAME).dll

R_EXE = $(RHOME)/bin/Rterm.exe

.PHONY: all makeMakedeps

include $(RHOME)/src/gnuwin32/MkRules

ifdef DEBUG
  DLLFLAGS=
  DEBUGFLAG=$(G_FLAG)
else
  DLLFLAGS=-s
  DEBUGFLAG=
endif

COPTFLAG=-O3 -Wall
FOPTFLAG=-O3

CPPFLAGS=$(PKG_CPPFLAGS) -I$(RHOME)/include
CFLAGS=$(PKG_CFLAGS) $(DEBUGFLAG) $(COPTFLAG)
FFLAGS=$(PKG_FFLAGS) $(DEBUGFLAG) $(FOPTFLAG)
DLLLIBS=-L$(RHOME)/$(IMPDIR) $(PKG_LIBS) -lR
RESFLAGS=-I $(RHOME)/include

CSOURCES=$(wildcard -f *.c)
FSOURCES=$(wildcard -f *.f)
OBJECTS = $(CSOURCES:.c=.o) $(FSOURCES:.f=.o)
#OBJECTS=$(foreach i,$(CSOURCES) $(FSOURCES),$(basename $i).o)
DEPS=$(CSOURCES:.c=.d)

RCOBJ=$(DLLNAME)_res.o

-include Makevars.win

$(DLLNAME)_res.rc:
	@echo "tools:::.makeDllRes(\"$(DLLNAME)\", \"$(RVER)\")" | R_DEFAULT_PACKAGES=tools $(R_EXE) --slave --vanilla > $@

$(DLLNAME)_res.o: $(DLLNAME)_res.rc $(RHOME)/include/Rversion.h

ifeq ($(wildcard $(DLLNAME)-win.def),$(DLLNAME)-win.def)
$(DLLNAME).dll : $(DLLNAME)-win.def $(OBJECTS) $(RCOBJ)
	$(DLL) -shared $(DLLFLAGS) -o $@ $^ $(DLLLIBS)
else
$(DLLNAME).dll : $(OBJECTS) $(RCOBJ)
endif

shlib-clean:
	@$(RM) -f $(DLLNAME)_res.rc $(OBJECTS) $(RCOBJ) *.d Makedeps


makeMakedeps: $(DEPS)
	@$(RM) Makedeps
ifneq ($(strip $(DEPS)),)
	-@cat $(DEPS) >> Makedeps
endif

-include Makedeps
